﻿using BMSPlatform.DeviceCommunication;
using DataManage;
using ServiceLayerManagement;

namespace BMSPlatform.ViewModel
{
    public class DeviceConnectedViewModel : BaseViewModel
    {
        #region 变量
        private PlatformConfig config;
        public PlatformConfig Config { get => config; set { config = value; NotifyPropertyChanged(); } }

        private bool canStart = false;
        /// <summary>
        /// can通道成功开启
        /// </summary>
        public bool CANStart { get => canStart; set { canStart = value; NotifyPropertyChanged(); } }
        #endregion

        /// <summary>
        /// 开启设备
        /// </summary>
        public void OnOpenDevice()
        {
            RespResult<string> respResult = DeviceConnected.OpenDevice(Config);
            if (respResult != null)
            {
                if (respResult.Code == 0)
                {
                    CANStart = true;
                    respResult.Message = "CAN链接成功";
                    GlobalData.PlatformConfig = SerializeHelper.DeepCopy(Config);
                }
                else
                {
                    CANStart = false;
                }
                MessageHelper.Info(respResult.Message);
            }
            else
            {
                CANStart = false;
            }
            GlobalData.CANConnectState = CANStart;
        }

        public void OnCloseDevice()
        {
            DeviceConnected.CloseDevice();
            CANStart = false;
        }
    }
}
